package com.example.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.database.LichkhamTB;
import com.example.datlichkham.Dangkithanhcong;
import com.example.datlichkham.R;
import com.example.dto.Lichkham;

public class LichKhamArrayAdapter extends ArrayAdapter<Lichkham> {
	ArrayList<Lichkham> arrData = null;
	Context mContext;
	int res;
	public LichKhamArrayAdapter(Context context, int resource, ArrayList<Lichkham> arrData) {
		super(context, resource, arrData);
		this.mContext = context;
		this.res = resource;
		this.arrData = arrData;
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		LichKhamHolder holder = null; 
		if(view==null)
		{
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			convertView = inflater.inflate(res, null);
			holder = new LichKhamHolder();
			holder.TenBacsi = (TextView) convertView.findViewById(R.id.tvTenBacsi);
			holder.Ngay = (TextView) convertView.findViewById(R.id.tvNgay);
			holder.Gio = (TextView) convertView.findViewById(R.id.tvGio);
			holder.TenChuyenkhoa = (TextView) convertView.findViewById(R.id.tvTenChuyenkhoa);
			holder.btnDatlich = (Button) convertView.findViewById(R.id.btnDatlich);
			convertView.setTag(holder);
		}
		else
		{
			holder= (LichKhamHolder)convertView.getTag();
					
		}
		
		
		final Lichkham lichkham = (Lichkham) arrData.get(position);
		holder.TenBacsi.setText(lichkham.getTenBacsi());
		holder.TenChuyenkhoa.setText(lichkham.getTenChuyenkhoa());
		holder.Ngay.setText(lichkham.getNgay());
		holder.Gio.setText(lichkham.getGio());
		if(lichkham.trangThai.equals(1))
		{
			holder.btnDatlich.setEnabled(false);
			holder.btnDatlich.setText("Lịch bận");
		}
		else
		{
			holder.btnDatlich.setEnabled(true);
			holder.btnDatlich.setText("Đặt Lịch");
		}
		holder.btnDatlich.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Intent intent = new Intent(mContext, Dangkithanhcong.class);
				intent.putExtra("ObjectLichKham", lichkham);
				LichkhamTB tb = new LichkhamTB(mContext);
				long kq = tb.updateStatusLichkham(lichkham.getMaLichkham(), ""+1);
				if(kq>0)
				{
					Toast.makeText(mContext, "Đã đặt lịch thành công!", Toast.LENGTH_SHORT).show();
					mContext.startActivity(intent);
				}
				else
				{
					Toast.makeText(mContext, "Có lỗi xảy ra trong quá trình đặt lịch!", Toast.LENGTH_SHORT).show();
				}
				
			}
		});
		
		return convertView;
	}
	private class LichKhamHolder
	{
		
		public TextView TenBacsi;
		public TextView Ngay;
		public TextView Gio;
		public TextView TenChuyenkhoa;
		public Button btnDatlich;
	}
}
